using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Advertisement.Resources;

namespace Advertisement
{
    public enum GameTitle
    {
        SpaceBattle,
        Enigma,
        EbookReader,
        Ideopuzzle,
        Crossroads,
        RisikoDice,
        Flashlight,
        Dice3D
    }

    public class AdsComponent : DrawableGameComponent
    {
        private List<GameTitle> game_to_ad;
        private Texture2D bg;
        private List<Texture2D> games_tile;
        private List<Vector2> tiles_position;
        private Vector2 bg_pos, fr_s, sn_s;
        private SpriteBatch spriteBatch;
        private SpriteFont font;

        private float time_before_exit, elapsed_time, bg_rot;

        public AdsComponent(Game game, List<GameTitle> game_to_ad, float time_before_exit)
            : base(game)
        {
            this.time_before_exit = time_before_exit;
            this.game_to_ad = game_to_ad;

            Strings.Culture = System.Globalization.CultureInfo.CurrentCulture;
        }

        public override void Initialize()
        {
            elapsed_time = time_before_exit;
            bg_rot = 0;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            var isLandscape = Game.GraphicsDevice.Viewport.Width > Game.GraphicsDevice.Viewport.Height ? true : false;
            var random = new Random(DateTime.Now.Millisecond);

            games_tile = new List<Texture2D>();
            tiles_position = new List<Vector2>();

            var n = Math.Min(5, game_to_ad.Count);

            for (int i = 1; i <= n; i++)
            {
                var ind = random.Next(game_to_ad.Count);

                games_tile.Add(GetGameTile(game_to_ad[ind]));
                tiles_position.Add(GetTilePosition(i));

                game_to_ad.RemoveAt(ind);
            }

            bg = Game.Content.Load<Texture2D>("../AdvertisementContent/Shared/ads_bg");
            font = Game.Content.Load<SpriteFont>("../AdvertisementContent/Fonts/Regular30");

            if (isLandscape)
            {
                bg_pos = Vector2.Zero;

                fr_s = new Vector2(190 - font.MeasureString(Strings.First_S).X / 2.0f, 60);
                sn_s = new Vector2(190 - font.MeasureString(Strings.Second_S).X / 2.0f, 110);
            }
            else
            {
                bg_rot = MathHelper.ToRadians(90);
                bg_pos = new Vector2(bg.Height, 0);

                fr_s = new Vector2(130 - font.MeasureString(Strings.First_S).X / 2.0f, 90);
                sn_s = new Vector2(130 - font.MeasureString(Strings.Second_S).X / 2.0f, 135);
            }

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            elapsed_time -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (elapsed_time <= 0)
                Game.Exit();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            spriteBatch.Draw(bg, bg_pos, null, Color.White, bg_rot, Vector2.Zero, 1, SpriteEffects.None, 0);

            for (int i = 0; i < games_tile.Count; i++)
                spriteBatch.Draw(games_tile[i], tiles_position[i], Color.White);

            spriteBatch.DrawString(font, Strings.First_S, fr_s, Color.White);
            spriteBatch.DrawString(font, Strings.Second_S, sn_s, Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private Texture2D GetGameTile(GameTitle g)
        {
            return Game.Content.Load<Texture2D>("../AdvertisementContent/GameTiles/" + g.ToString());
        }

        private Vector2 GetTilePosition(int i)
        {
            var isLandscape = Game.GraphicsDevice.Viewport.Width > Game.GraphicsDevice.Viewport.Height ? true : false;

            if (isLandscape)
            {
                switch (i)
                {
                    case 1:
                        return new Vector2(330, 16);
                    case 2:
                        return new Vector2(536, 16);
                    case 3:
                        return new Vector2(102, 243);
                    case 4:
                        return new Vector2(330, 243);
                    default:
                        return new Vector2(536, 243);
                }
            }
            else
            {
                switch (i)
                {
                    case 1:
                        return new Vector2(256, 31);
                    case 2:
                        return new Vector2(48, 276);
                    case 3:
                        return new Vector2(256, 276);
                    case 4:
                        return new Vector2(48, 542);
                    default:
                        return new Vector2(256, 542);
                }
            }
        }
    }
}
